home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: Easiest way to center a string?
- Date: Fri, 23 Feb 96 00:02:17 GMT
- Organization: none
- Distribution: world
- Message-ID: <825033737snz@genesis.demon.co.uk>
- References: <4fobka$1st@parlor.hiwaay.net> <DMqEG5.8z4@eskimo.com> <Pine.A32.3.91.960221162940.161086A-100000@black.weeg.uiowa.edu>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <Pine.A32.3.91.960221162940.161086A-100000@black.weeg.uiowa.edu>
- robinson@blue.weeg.uiowa.edu "The Amorphous Mass" writes:
-
- >On 21 Feb 1996, Tanmoy Bhattacharya wrote:
- >
- >> The Amorphous Mass <robinson@blue.weeg.uiowa.edu> writes:
- >> <snip>
- >> > In article Amorphous Mass <robinson@blue.weeg.uiowa.edu> writes:
- >> > <snip>
- >> > That's equivalent to strlen(String + -LineLen), which is in turn
- >> > equivalent to strlen(String[-LineLen]), and the expression
- > String[-80] is
- >>
- >> ^ this is now a char, not a char*.
- >
- > Certainly is. My apologies for slipping up here. For some
- >unaccountable reason I was thinking a[i] was equivalent to (a + i) and not
- >*(a + i).
- >
- >> > If you meant (strlen(String) - LineLen) / 2 then you will get a
- >> > negative length specifier for any string with fewer than LineLen
- >> > characters. If you meant (LineLen - strlen(String) / 2, then your
- >> > strlen returns size_t which is guaranteed unsigned. (I am assuming
- >> > LineLen is not long.)
- >
- >> Right, but strlen(String) would be 12, minus LineLen which is 80, which
- >> is -68, divided by two is -34, which is the number passed as a width
- >
- >> but if size_t is unsigned int or unsigned long, and LineLen is int,
- >> the result is not -68 because an unsigned quantity cannot be -68. (My
- >> parenthetical remark was wrong: I had assumed that size_t was at least
- >> as long as unsigned int and LineLen is not long).
- >
- > I guess I still need to have this clarified (my knowledge of type
- >promotion et al is admittedly shaky). If you subtract an unsigned int
- >from an int, is the result necessarily unsigned int?
-
- Yes, this follows from the rules for the 'usual arithmetic conversions'.
- These ensure 2 operands are promoted to a common type. I won't list them
- out in full right now but if one operand is a signed long and the other
- an unsigned long the result is unsigned long. Similarly for signed and
- unsigned int. In that respect I believe the ANSI rules match the old
- K&R rules.
-
- >Is that true
- >generally for integral types?
-
- All chars and shorts promote to int except where an int can't represent all
- of the values of an unsigned char or unsigned short. in that case it
- promotes to unsigned int. The other 'usual arithmetic conversion' rules are
- then applied to these promoted types.
-
- > IOW, are you saying that strlen("hello,
- >world") - 80 will evaluate to UINT_MAX - 68 (or something like that)?
-
- I made a mistake. -68U evaluates to (UINT_MAX+1)-68, not UINT_MAX-68.
-
- If size_t corresponds to unsigned int or unsigned long that is correct.
- If it corresponds to unsigned short or unsigned char and int is wider
- then the result will have type int and value -68. If you want to ensure that
- value you should write (int)sizeof("hello, world") - 80
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-